package com.mairuide.version_up.service;

import com.mairuide.version_up.dao.CompanyDao;
import com.mairuide.version_up.pojo.Company;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @auther 刘中华
 * @create 2019-03-08 10:45
 */
@Service
public class CompanyService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private CompanyDao companyDao;

    @Resource
    private RestTemplate restTemplate;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * @Author 刘中华
     * @Date 2019/3/8 10:50
     * @Param []
     * @return com.mairuide.version_up.pojo.Company
     * @Description  得到当前公司
     **/
    public Company getOne(){
        List<Company> all = (List<Company>)companyDao.findAll();
        return all.get(0);
    }

    public Map getVersion(){
        Integer flag = 1; //其实没啥用，但是远程的接口需要这个参数，随便写一个
        Map map = null;
        try {
            map = restTemplate.postForObject("http://47.105.197.249:8888/base-site/version/getlist"+"/"+flag, null, Map.class);
        } catch (RestClientException e) {
            System.out.println("获取远程版本报错");
            e.printStackTrace();
        }
        return map;
    }
    public Map getRemoteSql(String time){
        Map map = null;
        try {
            map = restTemplate.postForObject("http://47.105.197.249:8888/base-site/version/getsql/"+time, null, Map.class);
        } catch (RestClientException e) {
            System.out.println("获取远程sql报错");
            e.printStackTrace();
        }
        return map;
    }
    //专门的一个接口检测是否有网
    public Map isOnLine(){
        Map map = null;
        try {
            map = restTemplate.getForObject("http://47.105.197.249:8888/base-site/user/index", Map.class);
        } catch (RestClientException e) {
            System.out.println("检测有网没网报错");
            e.printStackTrace();
        }
        return map;
    }
    public Map getRemoteExe(String time){
        Integer flag = 1;
        Map map = restTemplate.postForObject("http://47.105.197.249:8888/base-site/file/is_down/"+time+"/"+flag, null, Map.class);
        if (Integer.valueOf(map.get("code").toString())==20000){
            map.put("url","http://47.105.197.249:8888/base-site/file/down"+"/"+flag);
        }else{
            map.put("url","已经是最新版本");
        }
        return map;
    }
    /**
     * @Author 刘中华
     * @Date 2019/3/8 11:25
     * @Param []
     * @return java.util.Map
     * @Description   得到当前版本好和远程的版本号
     **/
    public Map getCurrentVersion(){
        HashMap<String, Object> map = new HashMap<>();
        Company one = getOne();
        Map version = getVersion();
        Object o = version.get("data");
        map.put("current",one);
        map.put("remote",o);
        return map;
    }

    /**
     * @Author 刘中华
     * @Date 2019/3/8 11:25
     * @Param []
     * @return java.util.Map
     * @Description   获取远程的sql脚本并且执行脚本
     **/
    public Map upSql(String time) {
        Map map = null;
        try {
            map = getRemoteSql(time);
            List<String> sql_list = (List<String>) map.get("data");
            for (int i = 0; i < sql_list.size(); i++) {
                if (StringUtils.isBlank(sql_list.get(i))){
                    continue;
                }
                if (StringUtils.startsWith(sql_list.get(i),"#")){
                    continue;
                }
                try {
                    logger.info("执行的sql------》》》》"+sql_list.get(i));
                    jdbcTemplate.execute(sql_list.get(i));
                } catch (DataAccessException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
    /**
     * @Author 刘中华
     * @Date 2019/3/11 9:21
     * @Param []
     * @return void
     * @Description  更新公司信息
     **/
    public void updateCompanyInfo(Company company){
        Map currentVersion = getCurrentVersion();
        Map remote = (Map) currentVersion.get("remote");
        if (remote==null) return;
        String version = (String) remote.get("version_num");

        Company one = companyDao.findOne(company.getId());
        one.setUpdate_time(new Date());
        one.setVersion_num(version);
        companyDao.save(one);
    }
    /**
     * @Author 刘中华
     * @Date 2019/6/20 9:20
     * @Param []
     * @return boolean
     * @Description 检测是否有新的版本升级
     **/
    public boolean isHaveUp(){
        try {
            Map currentVersion = getCurrentVersion();
            Company current = (Company)currentVersion.get("current");
            Map<String,Object> remote = (Map<String,Object>)currentVersion.get("remote");
            String remote_version_num = (String)remote.get("version_num");
            String current_version_num = current.getVersion_num();
            if (remote_version_num.equals(current_version_num)){
                return false;
            }else{
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("检测是否有新的版本升级报错");
        }
        return false;
    }
}
